package com.bdqn;

import jakarta.annotation.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
 * UserControllerTester
 *
 * @author LILIBO
 * @since 2023-12-08
 */
@SpringBootTest
public class UserControllerTester {

    @Resource
    private WebApplicationContext wac; // 注入 Spring Web 上下文的环境

    private MockMvc mockMvc;

    @BeforeEach
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testGetUser() throws Exception {
        String mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/user/2")) // 向该地址发起测试请求
                .andReturn().getResponse().getContentAsString(); // 返回响应主体内容
        System.out.println("Result === " + mvcResult);
    }

}
